package com.knife.bbs.business;

import java.util.Date;
import java.util.List;


import com.knife.web.ActionContext;
import com.knife.web.tools.DbPageList;
import com.knife.web.tools.IPageList;
import com.knife.bbs.domain.BBSDoc;
import com.knife.bbs.domain.Vote;
import com.knife.bbs.domain.VoteItem;

public class VoteService extends DAOSupportService {	
	public Vote getVote(String cid){
	
		return (Vote)this.dao.get(Vote.class,cid);
	}
	
	public  Vote getVoteByTitle(String title){		
		return (Vote)this.dao.getBy(Vote.class,"title",title);
	}
	
	public Vote getVoteByDoc(String docid){		
		return (Vote)this.dao.getBy(Vote.class,"docid",docid);
	}	
	
	public boolean delVote(String cid)
	{
		Vote v=getVote(cid);
		List items=getVoteItems(v);
		for(int i=0;i<items.size();i++)
		{
		this.del(items.get(i));
		}
		return this.del(v);
	}
	public  List queryVote(String scope){
		return this.dao.query(Vote.class,scope);
	}

	public  VoteItem getVoteItem(String cid){		
		return (VoteItem)this.dao.get(VoteItem.class,cid);
	}	
	
	public  List queryVoteItem(String scope){
		return this.dao.query(VoteItem.class,scope);
	}
	
	public List getVoteItems(Vote vote)
	{
		String scope="voteId=?";
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(vote.getCid());
		return  this.dao.query(VoteItem.class,scope,paras);
	}
	
	public  IPageList queryVotes(String scope, int pageSize, int pageNo) {
		DbPageList pList = new DbPageList(Vote.class, scope, null);
		pList.doList(pageNo, pageSize);
		return pList;
	}
	
	
	public  boolean add(String dirSn,Vote vote,String[] voteitem){
		boolean ret=false;
		///BBSDir d=BBSDir.read(dir);
		BBSDoc doc=new BBSDoc();
		doc.setTitle(vote.getTitle()+"（投票）");
		doc.setIntro(vote.getInfo());
		doc.setDirsn(dirSn);
		doc.setInputuser(((ActiveUser)ActionContext.getContext().getSession().getAttribute("bbsuser")).getUserName());
		doc.setHasvote(new Integer(1));
		doc.setStatus(new Integer(0));
		doc.setInputtime(new Date());
		this.add(doc);
		vote.setDocid(doc.getCid());
		vote.setCid(null);
		this.add(vote);		
		for(int i=0;i<voteitem.length;i++){
			VoteItem vi=new VoteItem();
			vi.setInfo(voteitem[i]);
			vi.setVoteid(vote.getCid());
			vi.setVotenum(new Integer(0));
			this.add(vi);
		}
		return ret;
	}
}
